package com.sd.nacosdemo.service.impl;

import com.sd.nacosdemo.Util.MyMail;
import com.sd.nacosdemo.entity.Employee;
import com.sd.nacosdemo.dao.EmployeeDao;
import com.sd.nacosdemo.service.EmployeeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Employee)表服务实现类
 *
 * @author makejava
 * @since 2021-06-24 13:59:34
 */
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeDao employeeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param eid 主键
     * @return 实例对象
     */
    @Override
    public Employee queryById(Long eid) {
        return this.employeeDao.queryById(eid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Employee> queryAllByLimit(int offset, int limit) {
        return this.employeeDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Employee insert(Employee employee) {
        Date date = new Date();
        employee.setInTime(date);
        this.employeeDao.insert(employee);
        return employee;
    }

    /**
     * 修改数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Employee update(Employee employee) {
        Employee employee1 = this.employeeDao.queryById(employee.getTid());
        /*判断导师是否有导师资格*/
        if (employee1.getIsDard()==4){
            this.employeeDao.update(employee);
        }
        return this.queryById(employee.getEid());
    }

    /**
     * 通过主键删除数据
     *
     * @param eid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long eid) {
        return this.employeeDao.deleteById(eid) > 0;
    }

    /**
     * 发送邮件给新员工部门主管，给入职三天内的新员工分配导师
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public boolean isThreeDay() throws Exception {
        Employee employee = new Employee();
        //查询没被分配导师的新员工
        employee.setTid(0L);
        employee.setIsOfficial(0);
        employee.setIsDard(5);
        employee.setIsDel(0);
        List<Employee> employees = employeeDao.queryAll(employee);
        for (Employee employee1 : employees) {
            String code = "该员工已入职，请尽快为他（她）分配导师！谢谢配合！";
            String email = null;
            List<String> toEmail = new ArrayList<>();
            Date date = new Date();
            long time1 = date.getTime();
            Date inTime = employee1.getInTime();
            long time2 = inTime.getTime();
            Long time = time1 - time2;
            String name = employee1.getName();
            code = name +","+ code;//动态拼接邮箱内容
            //查询该员工的部门主管邮箱
            Employee em = new Employee();
            em.setIsDard(2);
            Long departId = employee1.getDepartId();
            em.setDepartId(departId);
            em.setIsDel(0);
            List<Employee> employees1 = employeeDao.queryAll(em);
            if (employees1.size()!=0){
                email = employees1.get(0).getEmall();
                toEmail.add("1787953976@qq.com");
                if (time <= 259200000L){
                    MyMail.mailLogin(code,email,toEmail);
                }
            }
        }
        return true;
    }
}
